package com.panopset.sf;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.KeyEvent;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.panopset.Alert;
import com.panopset.compat.MathUtil;
import com.panopset.compat.RezIO;
import com.panopset.op.Alarm;

public class AlarmSwingControls {
	private final Windoc w;

	public AlarmSwingControls(Windoc windoc) {
		w = windoc;
	}

	private int getHour() {
		return MathUtil.getIntValue(getHourField().getText());
	}

	private int getMinute() {
		return MathUtil.getIntValue(getMinField().getText());
	}

	private int getWarn() {
		return MathUtil.getIntValue(getWarnField().getText());
	}

	private JTextField hourField;

	private JTextField getHourField() {
		if (hourField == null) {
			hourField = w.cf.createTitledField(w, 8, "Hour", "Alarm hour",
					"alrmhr");
		}
		return hourField;
	}

	private JTextField warnField;

	private JTextField getWarnField() {
		if (warnField == null) {
			warnField = w.cf
					.createTitledField(
							w,
							8,
							"Warning",
							"Warning time, Alarm Clock will go off this many minutes before set time.",
							"2", "alrmwrn");
		}
		return warnField;
	}

	private JTextField minField;

	private JTextField getMinField() {
		if (minField == null) {
			minField = w.cf.createTitledField(w, 8, "Minute", "Alarm minute",
					"alrmmnt");
		}
		return minField;
	}

	private TitledTextArea msgTA;

	private TitledTextArea getMsgTA() {
		if (msgTA == null) {
			msgTA = w.cf.createTitledTextArea(w, "Message",
					"Enter message you want displayed", "msg");
		}
		return msgTA;
	}

	private TitledFileSelection soundFile;

	private TitledFileSelection getSoundfile() {
		if (soundFile == null) {
			soundFile = new TitledFileSelection(w, "Sound file",
					"Select a sound file to accompany alarm",
					JFileChooser.FILES_ONLY, JFileChooser.OPEN_DIALOG, "sndf");
		}
		return soundFile;
	}

	private JPanel p;

	public JPanel getPanel() {
		if (p == null) {
			p = new JPanel(new BorderLayout());
			JPanel np = new JPanel(new FlowLayout());
			np.add(getHourField());
			np.add(getMinField());
			np.add(new ThreadButton() {

				@Override
				public void run() {

					if (getSoundfile().getFile().exists()) {
						new Thread(new Runnable() { // the wrapper thread is
													// unnecessary, unless it
													// blocks on the Clip
													// finishing, see comments
									public void run() {
										try {
											Clip clip = AudioSystem.getClip();
											AudioInputStream inputStream = AudioSystem
													.getAudioInputStream(getSoundfile()
															.getFile());
											clip.open(inputStream);
											clip.start();
										} catch (Exception e) {
											Alert.red("Please try a wav file, not supported:"
													+ RezIO.getCanonicalPath(getSoundfile()
															.getFile()));
										}
									}
								}).start();
					}

					Alarm a = new Alarm();
					a.setWarning(getWarn() * 1000 * 60);
					a.setHour(getHour());
					a.setMinute(getMinute());
					a.setMessage(getMsgTA().getText());
					a.setMonitor(this);
					a.runInline();
				}

				@Override
				public String createTipText() {
					return "Set alarm clock";
				}

				@Override
				public String createThreadName() {
					return "Alarm thread";
				}

				@Override
				public String createText() {
					return "Set";
				}

				@Override
				public char createMnemonic() {
					return KeyEvent.VK_S;
				}
			}.getGui());
			np.add(getWarnField());
			p.add(BorderLayout.NORTH, np);
			p.add(BorderLayout.CENTER, getMsgTA().getPanel());
			p.add(BorderLayout.SOUTH, getSoundfile().getPanel());
		}
		return p;
	}

}
